layui.use(['layer', 'table'], function () {
    let table = layui.table,
        layer = layui.layer;
    layui.layer.load(0, {shade: [0.5, '#fff']});


    /*查询所有学生自选课题的信息*/
    table.render({
        elem: '#task-student-table'
        , toolbar: '#task-student-table-toolbar'
        , height: 700
        , initSort: {field: 'id', type: 'desc'}
        , url: '/getTitle'
        , where: {queryKey: '', type: 1}
        , cols: [[ //标题栏
            {field: 'xtId', title: '编号', width: 90, align: 'center', sort: true, fixed: 'left'},
            {field: 'title', title: '标题', width: 200, align: 'center', fixed: 'left'},
            {field: 'no', title: '学号', width: 150, align: 'center', fixed: 'left'},
            {field: 'sname', title: '姓名', width: 130, align: 'center'},
            {field: 'collegeName', title: '所属学院', width: 130, align: 'center'},
            {field: 'majorName', title: '所属专业', width: 150, align: 'center'},
            {field: 'tName', title: '指导老师', width: 150, align: 'center'},
            {field: 'tno', title: '老师工号', width: 150, align: 'center'},
            {field: 'addtime', title: '申请时间', width: 130, align: 'center'},
            {field: 'from', title: '课题来源', width: 120, align: 'center'},
            {field: 'categoty', title: '课题类别', width: 100, align: 'center'},
            {field: 'nature', title: '课题性质', width: 140, align: 'center'},
            {field: 'intr', title: '课题简介', width: 240, align: 'center'},
            {field: 'opinoin', title: '审核意见', align: 'center', width: 120, hide: true},
            {
                field: 'state', title: '审核状态', width: 140, align: 'center', fixed: 'right', templet: function (res) {
                    if (res.state === '审核完成') {
                        return '<span style="color: green">审核完成</span>';
                    } else {
                        return '<span style="color: #1E9FFF">审核中</span>';
                    }
                }
            },
            {
                field: 'isPass', title: '是否通过', width: 140, align: 'center', fixed: 'right', templet: function (res) {
                    if (res.isPass === '不通过') {
                        return '<span style="color: red">不通过</span>';
                    } else if (res.isPass === '通过') {
                        return '<span style="color: green">通过</span>';
                    } else {
                        return '<span style="color: #1E9FFF">审核中</span>';
                    }
                }
            },
            {title: '操作', align: 'center', width: 150, toolbar: '#msg-table-tool', fixed: 'right'}
        ]]
        // ,skin: 'line' //表格风格
        , even: true
        , page: true
        , limits: [13, 26, 39]
        , limit: 13
        , text: {
            none: '<div class="layui-anim layui-anim-scaleSpring"  style="margin-top: 150px"><i class="layui-icon"  style="color: #009f95;font-size: 150px;border-bottom: #009f95 solid 2px;padding:15px;margin-top: 50px" >&#xe664;</i><br>' +
                '<br><br><br><br><div class="layui-text" style="font-size: 25px;margin-top: 25px;color: #009f95">好像没有没找到数据呢</div></div>'
            ,
            error: '<div style="margin-top: 150px"><i class="layui-icon"  style="color: #009f95;font-size: 150px;border-bottom: #009f95 solid 2px;padding:15px;margin-top: 50px" >&#xe664;</i><br>' +
                '<br><div class="layui-text" style="font-size: 25px;margin-top: 25px;color: #009f95">好像没有出错了呢</div></div>'
        }
    });


    //监听表头事件  监听对象  task-student-table
    table.on('toolbar(task-student-table)', function (obj) {
        //查询事件
        if (obj.event === 'query') {  //按关键字搜索事件
            let queryKey = $('#queryKey').val().trim(); //获取搜索的关键字
            if (queryKey === null || queryKey === '') {
                layer.msg('请输入要查询工号/姓名/课题名称', {offset: '250px', time: 1500, anim: 6});
                return false;
            }
            let major = $('#major').val(); // 获取专业
            if (major === null || major === '') {
                layer.msg('请选择专业', {offset: '250px', time: 1500, anim: 6});
                return false;
            }

            table.reload('task-student-table', {
                where: {queryKey: queryKey, type: 1} //设定异步数据接口的额外参数
            });

        } else if (obj.event === 'queryAll') {  //查询全部
            table.reload('task-student-table', {
                where: {queryKey: '', type: 1} //设定异步数据接口的额外参数
            });
        }
    });


    //监听操作栏事件  task-student-table
    table.on('tool(task-student-table)', function (obj) {
        let data = obj.data;  //json格式的数据
        if (obj.event === 'detail') {  //编辑事件
            layer.open({
                type: 1
                , offset: 50
                , skin: 'layui-layer-rim'
                , area: ['740px', '600px']
                , content: $("#task-detail")
                , title: '学生申请课题详情内容'
                , success: function (layerObj) {
                    $(".layui-layer-shade").appendTo(layerObj.parent());
                    setTaskDetail(data);
                }
                , end: function () {
                    $("#task-detail").css("display", 'none');
                }
            });
        } else if (obj.event === 'check') {//审核
            //判断是否已经审核完毕
            if (data.state === '审核完成') {
                layer.msg('该学生选题已经审核完毕', {offset: '250px', time: 1500, anim: 6});
                return false;
            }
            layer.open({
                type: 1
                , offset: 0
                , skin: 'layui-layer-rim'
                , area: ['740px', '800px']
                , content: $("#check-task-student")
                , title: '审核学生自选题目'
                , success: function (layerObj) {
                    $(".layui-layer-shade").appendTo(layerObj.parent());
                    /*设置回显数据*/
                    setTaskDetail(data);
                    $("#task-check-student-tbody").html($("#task-detail-tbody").html());
                }
                , end: function () {
                    $("#check-task-student").css("display", 'none');
                }
            });


        }
    });

    layui.layer.closeAll('loading');


    //通过审核
    $("#check-pass").click(function () {
        let result = '通过';
        checkResult(result);
    });

    //不通过审核
    $("#check-no-pass").click(function () {
        let result = '不通过';
        checkResult(result);
    });

});

//设置详情
function setTaskDetail(data) {
    $("#task-intr").html(data.intr);
    $("#task-from").html(data.from);
    $("#task-category").html(data.categoty);
    $("#task-addtime").html(data.addtime);
    $("#task-nature").html(data.nature);
    $("#task-college").html(data.collegeName);
    $("#task-major").html(data.majorName);
    $("#task-tname").html(data.TName);
    $("#task-state").html(data.state);
    $("#task-sno").html(data.no);
    $("#task-id").html(data.xtId);
    $("#task-title").html(data.title);
    $("#task-tno").html(data.tno);
    $("#task-sname").html(data.sname);
    $("#task-opinion").html(data.opinoin);
}


//提交获取审核结果
function checkResult(result) {
    let opinion = $("#check-opinion").val().trim();
    if (opinion === null || opinion === ''){
        layui.layer.msg('请填写审核意见', {offset: '250px', time: 1500, anim: 6});
        return false;
    }
    layui.layer.closeAll('page');
    let id = $("#task-id").html().trim();
    let loadPage = layui.layer.load(0, {shade: [0.5, '#fff']});
    $.ajax({
        url: "/checkTitle",
        type: 'post',
        data: {
            result: result, opinion: opinion,id:id
        },
        dataType: 'json',
        success: function (data) {  //调用接口成功
            if (data === true || data === 'true') {
                layer.close(loadPage);
                layer.closeAll('page');
                layui.layer.msg('审核成功！', {offset: '300px', time: 2500, anim: 0});
                layui.table.reload('task-student-table', {
                    where: {queryKey: '', type: 1}
                });
            } else {
                layui.layer.close(loadPage);
                layui.layer.alert('审核失败！');
            }
        },
        error: function () {  //接口错误
            layui.layer.close(loadPage);
            layui.layer.alert('服务器出错啦,请稍后再试！');
        }
    });
}

